<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceControllerCurrency extends JController
{
	/**
	 * Controller construct
	 *
	*/
	function __construct(){
		parent::__construct();
		
		// Register Extra task
		$this->registerTask( 'add'  ,		 	'edit' );
	}

	/**
	 * Method to get detail currency
	 *
	 * @return void
	 * @since 1.0
	*/
	function detail() {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('currency', 'view', 'currency', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('currency', 'html');

		// Get/Create the model
		$model = & $this->getModel('Currency');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('default');

		// Display the view
		$view->display();
	}

	/**
	 * Method to get form  edit currency
	 *
	 * @return void
	 * @since 1.0
	 */
	function edit(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		// get vars
		$id		= JRequest::getVar('id', 0, 'post', 'int');
		
		if($user->gid < 25) {

			$canEdit	= Ownservice::checkAccess('currency', 'edit', 'currency', 'all');
			if ( (!$canEdit) && ($id > 0) ) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
			
			$canAdd		= Ownservice::checkAccess('currency', 'add', 'currency', 'all');
			if ( (!$canAdd) && ($id < 1) ) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
			
		}

		// Create the view
		$view = & $this->getView('currency', 'html');

		// Get/Create the model
		$model = & $this->getModel('Currency');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();

	}

	/**
	 * Logic to save customer account
	 * 
	 * @access public
	 * @return void
	*/
	function save(){

		//Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		// Get vars
		$post = JRequest::get( 'post' );

		$model = $this->getModel('Currency');

		$type = '';

		if($model->store($post)){
			$msg = JText::_('OWNSERVICE_CURRENCY_SAVED');
		} else {
			$type = 'error';
			$msg = JText::_('OWNSERVICE_ERROR_SAVING_CURRENCY');
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=currency');
		
		$this->setRedirect($link, $msg, $type);
	}
	
	/**
	 * Method to delete currency
	 * 
	 * @return void
	 * @since 1.0
	 */
	 function delete()
	 {
	 	global $mainframe;
		
	 	//Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');
		
		$user 	= & JFactory::getUser();
		
		$id 	= JRequest::getVar('id', 0, 'post', 'int');
		
		$model 	= $this->getModel('Currency');
		
		if( $user->gid <25 ) {
			$canDelete		= Ownservice::checkAccess('currency', 'delete', 'currency', 'all');
			if (!$canDelete) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}
		
		
		if(!$model->candelete($id))
		{
			JError::raiseWarning(500, JText::_('OWNSERVICE_YOU_CANNOT_REMOVE_THIS_CURRENCY_THERE_ARE_STILL_ITEMS_ASSOCIATED'));
		}
		else
		{
			if(!$model->delete($id))
			{
				JError::raiseWarning(500, JText::_('OWNSERVICE_OPERATION_FAILED'));
			}
			
			$msg = JText::_('OWNSERVICE_CURRENCY_DELETED');
		}
		
		$this->setRedirect( JRoute::_('index.php?option=com_ownservice&view=currency'), $msg );
	 }

	/**
	 * logic for cancel an action
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$app = & JFactory::getApplication();

		$link = JRoute::_('index.php?option=com_ownservice&view=currency');

		$this->setRedirect( $link );
	}
}